package com.ruoyi.mentor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mentor.bo.StudentAddBo;
import com.ruoyi.mentor.bo.StudentEditBo;
import com.ruoyi.mentor.bo.StudentQueryBo;
import com.ruoyi.mentor.domain.Instructor;
import com.ruoyi.mentor.domain.Spot;
import com.ruoyi.mentor.domain.Student;
import com.ruoyi.mentor.service.IStudentService;
import com.ruoyi.mentor.service.impl.ChooseServiceImpl;
import com.ruoyi.mentor.service.impl.ExamineServiceImpl;
import com.ruoyi.mentor.service.impl.InstructorServiceImpl;
import com.ruoyi.mentor.service.impl.SpotServiceImpl;
import com.ruoyi.mentor.vo.ExportStudentVo.ExportStudentVo;
import com.ruoyi.mentor.vo.StudentDetailInfoVo;
import com.ruoyi.mentor.vo.StudentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 研究生信息Controller
 *
 * @author LH
 * @date 2021-05-21
 */
@Api(value = "研究生信息控制器", tags = {"研究生信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/mentor/student")
public class StudentController extends BaseController {

    private final IStudentService iStudentService;

    @Autowired
    ExamineServiceImpl examineService;



    /**
     * 查询研究生信息列表
     */
    @ApiOperation("查询研究生信息列表")
    @PreAuthorize("@ss.hasPermi('mentor:student:list')")
    @GetMapping("/list")
    public TableDataInfo<StudentVo> list(@Validated StudentQueryBo bo) {
        TableDataInfo<StudentVo> studentVoTableDataInfo = iStudentService.queryPageList(bo);
        return studentVoTableDataInfo;
    }

    /**
     * 导出研究生信息列表
     */
    @ApiOperation("导出研究生信息列表")
    @PreAuthorize("@ss.hasPermi('mentor:student:export')")
    @Log(title = "研究生信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(@Validated StudentQueryBo bo) {
        List<ExportStudentVo> list = iStudentService.queryList(bo);
        ExcelUtil<ExportStudentVo> util = new ExcelUtil<ExportStudentVo>(ExportStudentVo.class);
        return util.exportExcel(list, "研究生信息");
    }

    @ApiOperation("导出导师的研究生信息列表")
    @Log(title = "导师的研究生信息", businessType = BusinessType.EXPORT)
    @GetMapping("/exportStudentByInsId")
    public AjaxResult exportStudentByInsId(@RequestParam("insId") Long insId) {
        List<ExportStudentVo> list = iStudentService.exportStudentByInsId(insId);
        ExcelUtil<ExportStudentVo> util = new ExcelUtil<ExportStudentVo>(ExportStudentVo.class);
        return util.exportExcel(list, "研究生信息");
    }





    /**
     * 获取研究生信息详细信息
     */
    @ApiOperation("获取研究生信息详细信息")
    @PreAuthorize("@ss.hasPermi('mentor:student:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("id") Long id) {
        return AjaxResult.success(iStudentService.queryById(id));
    }

    /**
     * 新增研究生信息
     */
    @ApiOperation("新增研究生信息")
    @PreAuthorize("@ss.hasPermi('mentor:student:add')")
    @Log(title = "研究生信息", businessType = BusinessType.INSERT)
    @PostMapping()
    public AjaxResult add(@Validated @RequestBody StudentAddBo bo) {
        return toAjax(iStudentService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改研究生信息
     */
    @ApiOperation("修改研究生信息")
    @PreAuthorize("@ss.hasPermi('mentor:student:edit')")
    @Log(title = "研究生信息", businessType = BusinessType.UPDATE)
    @PutMapping()
    public AjaxResult edit(@Validated @RequestBody StudentEditBo bo) {
        return toAjax(iStudentService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除研究生信息
     */
    @ApiOperation("删除研究生信息")
    @PreAuthorize("@ss.hasPermi('mentor:student:remove')")
    @Log(title = "研究生信息" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] ids) {
        return toAjax(iStudentService.deleteWithValidByIds(Arrays.asList(ids), true) ? 1 : 0);
    }

    /**
     * 获取研究生详细信息
     */
    @ApiOperation("获取研究生详细信息")
    @GetMapping("/detail")
    public AjaxResult getStudentDetailInfo(@RequestParam("studentId") Long studentId) {
        StudentDetailInfoVo studentDetail = iStudentService.getStudentDetailInfo(studentId);
        return AjaxResult.success(studentDetail);
    }

    /**
     * 根据用户id获取研究生详细信息
     */
    @ApiOperation("根据用户id获取研究生详细信息")
    @GetMapping("/detail/{userId}")
    public AjaxResult getStudentDetailByUserId(@PathVariable("userId") Long userId) {
        StudentDetailInfoVo studentDetail = iStudentService.getStudentDetailInfoByUserId(userId);
        if (studentDetail==null){
            return AjaxResult.error();
        }else {
            return AjaxResult.success(studentDetail);
        }
    }

    /**
     * 添加研究生详细信息
     */
    @ApiOperation("添加研究生详细信息")
    @PostMapping("/addStudentInfo")
    @Log(title = "添加研究生详细信息" , businessType = BusinessType.INSERT)
    public AjaxResult addStudentInfo(@RequestBody StudentDetailInfoVo studentDetailInfoVo) {
//        System.out.println(studentDetailInfoVo);
        if (studentDetailInfoVo==null){
            return AjaxResult.error("出错了");
        }
        iStudentService.saveStudent(studentDetailInfoVo);
        return AjaxResult.success();
    }


    /**
     * 管理员审核
     */
    @ApiOperation("管理员审核")
    @PostMapping("/examine")
    public AjaxResult examine(@RequestParam("adminName")String adminName,@RequestParam("status") Integer status,@RequestParam("studentId") Long studentId,@RequestParam("message") String message) {
        examineService.examine(adminName,status,studentId,message);
        return AjaxResult.success();
    }


    /**
     * 导师选择
     * @param type 1学生主动选择，2管理员强制选择
     */
    @ApiOperation("导师选择")
    @PostMapping("/chooseIns")
    @Log(title = "导师选择",businessType = BusinessType.OTHER)
    public synchronized AjaxResult chooseIns(@RequestParam("insId") Long insId,@RequestParam("stuId") Long stuId,@RequestParam("subId")Long subId,@RequestParam("type") Integer type) {
        iStudentService.chooseIns(insId,stuId,subId,type);
        return AjaxResult.success();
    }

    /**
     * 取消导师选择
     */
    @ApiOperation("取消导师选择")
    @PostMapping("/cancelChooseIns")
    @Log(title = "取消导师选择",businessType = BusinessType.OTHER)
    public synchronized AjaxResult cancelChooseIns(@RequestParam("insId") Long insId,@RequestParam("stuId") Long stuId,@RequestParam("subId") Long subId) {
        iStudentService.cancelChooseIns(insId,stuId,subId);
        return AjaxResult.success();
    }

    /**
     * 老师拒绝后，学生需要点击确认按钮，才可继续选择导师
     */
    @ApiOperation("老师拒绝后，学生需要点击确认按钮，才可继续选择导师")
    @PostMapping("/confirmStatus")
    @Log(title = "老师拒绝后，学生需要点击确认按钮，才可继续选择导师",businessType = BusinessType.OTHER)
    public synchronized AjaxResult confirmStatus(@RequestParam("insId") Long insId,@RequestParam("stuId") Long stuId) {
        iStudentService.confirmStatus(insId,stuId);
        return AjaxResult.success();
    }







}
